#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <stdexcept>

class Cpp_NextCompiler {
public:
    Cpp_NextCompiler(const std::string& code) : code_(code) {}

    void compile(const std::string& outputFilename) {
        std::ofstream outFile(outputFilename);
        if (!outFile.is_open()) {
            throw std::runtime_error("Failed to open output file.");
        }

        // 写 C++ 代码头部
        outFile << "#include <iostream>\n";
        outFile << "#include <unordered_map>\n";
        outFile << "#include <functional>\n";
        outFile << "int main() {\n";
        outFile << "    std::unordered_map<std::string, int> variables;\n";
        outFile << "    std::unordered_map<std::string, std::function<int(int)>> functions;\n";

        // 解析 Cpp_Next 代码并生成 C++ 代码
        std::istringstream stream(code_);
        std::string line;
        while (std::getline(stream, line)) {
            auto tokens = tokenize(line);
            if (tokens.empty()) continue;

            if (tokens[0] == "let" && tokens.size() == 3) {
                outFile << "    variables[\"" << tokens[1] << "\"] = " << tokens[2] << ";\n";
            } else if (tokens[0] == "print") {
                outFile << "    std::cout << ";
                for (size_t i = 1; i < tokens.size(); ++i) {
                    if (variables_.count(tokens[i])) {
                        outFile << "variables[\"" << tokens[i] << "\"]";
                    } else {
                        outFile << "\"" << tokens[i] << "\"";
                    }
                    if (i < tokens.size() - 1) {
                        outFile << " << \" \" << ";
                    }
                }
                outFile << " << std::endl;\n";
            } else if (tokens[0] == "define" && tokens.size() == 4) {
                outFile << "    functions[\"" << tokens[1] << "\"] = [](int " << tokens[2] << ") { return " << tokens[3] << "; };\n";
            } else if (tokens[0] == "call" && tokens.size() == 3) {
                outFile << "    variables[\"" << tokens[1] << "\"] = functions[\"" << tokens[2] << "\"](variables[\"" << tokens[3] << "\"]);\n";
            }
        }

        // 写 C++ 代码尾部
        outFile << "    return 0;\n";
        outFile << "}\n";
        outFile.close();
    }

private:
    std::vector<std::string> tokenize(const std::string& str) {
        std::vector<std::string> tokens;
        std::istringstream stream(str);
        std::string token;
        while (stream >> token) {
            tokens.push_back(token);
        }
        return tokens;
    }

    std::unordered_map<std::string, int> variables_;
    std::string code_;
};

int main() {
    const std::string simpleScriptCode = R"(
        let x 10
        let y 20
        define add a b a + b
        let sum call add x y
        print The sum of x and y is sum
    )";

    Cpp_NextCompiler compiler(simpleScriptCode);
    compiler.compile("output.cpp");

    // 使用 g++ 编译生成的 C++ 代码
    system("g++ output.cpp -o output");
    system("./output");

    return 0;
}

